package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentCommParametersDetail;
import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepTaskDetail;
import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepTaskDetailParam;
import com.mxpio.erp.common.equipment.service.EquipmentCommParametersDetailService;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepTaskDetailParamService;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepTaskDetailService;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 保养任务明细Service业务层处理
 *
 * @author Cheng
 * @date 2023-07-28
 */
@Service
public class EquipmentUpkeepTaskDetailServiceImpl extends BaseServiceImpl<EquipmentUpkeepTaskDetail> implements EquipmentUpkeepTaskDetailService {
    @Resource
    private EquipmentUpkeepTaskDetailParamService upkeepTaskDetailParamService;
    @Resource
    private EquipmentCommParametersDetailService equipmentCommParametersDetailService;

    @Override
    @Transactional
    public void saveDetail(EquipmentUpkeepTaskDetail upkeepTaskDetail) {
        save(upkeepTaskDetail);
        // 处理计划明细中的选项数据
        if (upkeepTaskDetail.getChooseEnable() == 1) {
            List<EquipmentCommParametersDetail> tpmUpkeepDetailParams = equipmentCommParametersDetailService.selectByMainId(upkeepTaskDetail.getParamId());
            List<EquipmentUpkeepTaskDetailParam> upkeepTaskDetailParams = tpmUpkeepDetailParams.stream().map(categoryParam -> {
                EquipmentUpkeepTaskDetailParam upkeepTaskDetailParam = new EquipmentUpkeepTaskDetailParam();
                upkeepTaskDetailParam.setBizNo(upkeepTaskDetail.getBizNo());
                upkeepTaskDetailParam.setNum(categoryParam.getNum());
                upkeepTaskDetailParam.setParam(categoryParam.getParameter());
                upkeepTaskDetailParam.setDetailId(upkeepTaskDetail.getId());
                return upkeepTaskDetailParam;
            }).collect(Collectors.toList());
            upkeepTaskDetailParamService.save(upkeepTaskDetailParams);
        }
    }

    @Override
    @Transactional
    public void updateDetail(EquipmentUpkeepTaskDetail upkeepTaskDetail) {
        update(upkeepTaskDetail);
        upkeepTaskDetailParamService.deleteMain(upkeepTaskDetail.getId());
        // 处理计划明细中的选项数据
        if (upkeepTaskDetail.getChooseEnable() == 1) {
            List<EquipmentCommParametersDetail> tpmUpkeepDetailParams = equipmentCommParametersDetailService.selectByMainId(upkeepTaskDetail.getParamId());
            List<EquipmentUpkeepTaskDetailParam> upkeepTaskDetailParams = tpmUpkeepDetailParams.stream().map(categoryParam -> {
                EquipmentUpkeepTaskDetailParam upkeepTaskDetailParam = new EquipmentUpkeepTaskDetailParam();
                upkeepTaskDetailParam.setBizNo(upkeepTaskDetail.getBizNo());
                upkeepTaskDetailParam.setNum(categoryParam.getNum());
                upkeepTaskDetailParam.setParam(categoryParam.getParameter());
                upkeepTaskDetailParam.setDetailId(upkeepTaskDetail.getId());
                return upkeepTaskDetailParam;
            }).collect(Collectors.toList());
            upkeepTaskDetailParamService.save(upkeepTaskDetailParams);
        }
    }
}
